package com.spring.test.utils;

import org.apache.shiro.crypto.hash.SimpleHash;

import java.util.Random;

/**
 * @author huangrusheng
 * @version 1.0
 * @date 2021/5/21 16:40
 */

/**
 * MD5工具
 */
public class MD5Utils {
    /**
     * 哈希散列算法的名字
     */
    public final static String HASH_ALGORITHM_NAME = "MD5";

    /**
     * 哈希加密迭代次數
     */
    public final static int HASH_ITERATIONS = 18;

    /**
     * 哈希加密盐值
     */
    public final static String HASH_SALT = "1deb6cc7586c471a8a6bb8530f820904";

    //92987c478fb2d335f74ca70c97fc6800
    /**
     *
     * 将密码明文根据salt盐值进行加密
     *
     * @param password
     * @return
     */
    public static String encryptPassword(String password) {
        String encryptPassword = new SimpleHash(HASH_ALGORITHM_NAME, password,HASH_SALT,HASH_ITERATIONS).toString();
        return encryptPassword;
    }

    private static char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     *自定义简单生成盐，是一个随机生成的长度为16的字符串，每一个字符是随机的十六进制字符
     */
    public static String salt() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(16);
        for (int i = 0; i < sb.capacity(); i++) {
            sb.append(hex[random.nextInt(16)]);
        }
        return sb.toString();
    }



}








